package pers.wq.controller;

import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import pers.wq.model.MyModel;
import pers.wq.model.bean.BaseBean;
import pers.wq.model.bean.College;
import pers.wq.util.StringUtil;

public class PublicListAction extends BaseIndexAction{
	private String xmlPath = "thList.xml";
	private BaseListData dataList;
	private String dataListId;
	private String method;
	private int userid;
	public PublicListAction(String beanId){
		super(beanId);
		System.out.println("PublicListAction __construct");
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		String reStr = ERROR;
		if(!StringUtil.strIsNull(dataListId)){
			if(dataListId.equals("user")) {
				System.out.println("PublicListAction curAction " + this.getCurAction());
				System.out.println("PublicListAction dataListId " + dataListId);
				setUserid(Integer.parseInt(BaseIndexAction.getSession(BaseIndexAction.ID_TAG)));
				return CHAIN;
			}
			ApplicationContext factory = new ClassPathXmlApplicationContext(getXmlPath());
			System.out.println("PublicListAction " + dataListId);
			dataList = (BaseListData) factory.getBean(dataListId);
			String idStr = getSession(ID_TAG);
			int id = null == idStr ? 0 : Integer.parseInt(idStr);
			dataList.setId(id);
			if(dataList.initTrItemList()){
				reStr = SUCCESS;
			}
		}
		System.out.println("PublicListAction execute reStr" + reStr);
		return reStr;
	}
	
	protected String formTag(){
		return null;
	}
	
	protected Map<String,String> _mapTag(BaseBean[] objs){
		System.out.println("_mapTag");
		for (int i = 0 ; i < objs.length ; i++) {
			BaseBean baseBean = objs[i];
			if(null != baseBean){
				System.out.println("_mapTag" + baseBean);
				return baseBean.tagMap();
			}
		}
		System.out.println("_mapTag over!!");
		return null;
	}
	public Map<String,String> mapTag(){
		return null;
	}
	
	public String sa() throws Exception{
		method = "add";
		System.out.println("showAdd" + method);
		return EA;
	}
	public String se() throws Exception{
		method = "update";
		System.out.println("showEdit" + method);
		return EA;
	}
	
	protected String _se(BaseBean[] bb){
		for (int i = 0 ; i < bb.length ; i++) {
			BaseBean baseBean = bb[i];
			if(null != baseBean){
				System.out.println("_se2 if in" + baseBean);
				bb[i] = baseBean.initBean();
				return EA;
			}
		}
		return ERROR;
	}
	protected boolean checkAndSaveObj(BaseBean obj){
		if(null != obj){
			MyModel.save(obj);
			return true;
		}
		return false;
	}
	
	protected boolean checkAndUpdateObj(BaseBean obj){
		if(null != obj){
			MyModel.update(obj);
			return true;
		}
		return false;
	}
	
	protected boolean checkAndDeleteObj(BaseBean obj){
		if(null != obj){
			return MyModel.delete(obj);
		}
		return false;
	}
	
	protected String _update(BaseBean[] objs) throws Exception{
		for (BaseBean object : objs) {
			if(checkAndUpdateObj(object)){
				return SUCCESS;
			}
		}
		return ERROR;
	}
	
	protected String _add(BaseBean[] objs) throws Exception{
		for (BaseBean object : objs) {
			if(checkAndSaveObj(object)){
				return SUCCESS;
			}
		}
		return ERROR;
	}
	
	protected String _delete(BaseBean[] objs) throws Exception{
		System.out.println("_delete in");
		for (BaseBean object : objs) {
			if(checkAndDeleteObj(object)){
				System.out.println("_delete for if in success" + object);
				return SUCCESS;
			}
		}
		System.out.println("_delete in error");
		return ERROR;
	}
	public String add() throws Exception{
		return this.execute();
	}
	public String update() throws Exception{
		return this.execute();
	}
	public String delete() throws Exception{
		return this.execute();
	}
	public BaseListData getDataList() {
		return dataList;
	}
	public void setDataList(BaseListData dataList) {
		this.dataList = dataList;
	}
	public String getDataListId() {
		return dataListId;
	}
	public void setDataListId(String dataListId) {
		this.dataListId = dataListId;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public String getXmlPath() {
		return xmlPath;
	}
	public void setXmlPath(String xmlPath) {
		this.xmlPath = xmlPath;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}
}
